<?php

/**
 * 用户提现记录控制器
 */

namespace App\Http\ManagerApi\Controllers;

use App\Api\BaseClass\Controller;
use App\Http\ManagerApi\Models\CashRequestLog;
use Illuminate\Support\Facades\DB;
use App\Http\ManagerApi\Models\User;
use App\Http\ManagerApi\Models\UserIntegralLog;

class CashController extends Controller
{
    /**
     * 用户提现列表
     */
    public function listCashLog()
    {
        $rules = [
            ['offset', 'required|integer'],
            ['limit', 'required|integer'],
            ['user_name', 'string'],
            ['mobile', 'string'],
            ['status', 'min:0|max:2'],
        ];

        $search = [
            ['where', 'user_name', 'like', '$user_name'],
            ['where', 'mobile', 'like', '$mobile'],
            ['where', 'status', '$status'],
            ['orderBy', 'created_at', 'desc']
        ];

        $format = [
            'user_name' => '%$user_name%',
            'mobile' => '%$mobile%'
        ];

        $params = $this->form->camelFormOrFail($rules);

        return $this->form->getMessage(with(new CashRequestLog)->pagination($params, $search, $format));
    }

    /**
     * 提现详情
     */
    public function cashLogDetail()
    {
        $rules = [['id', 'required|integer']];

        $params = $this->form->camelFormOrFail($rules);

        $cash = CashRequestLog::findOrFail($params['id']);

        $log = UserIntegralLog::where('cash_log_id', $params['id'])
            ->where('type', 7)
            ->where('is_finished', 1)
            ->first();
        if (isset($log)) {
            $cash->reason = $log->reason;
        }

        return $this->form->datas($cash);
    }

    /**
     * 删除提现记录
     */
    public function deleteCashLog()
    {
        $rules = [['id', 'required|integer']];

        $params = $this->form->camelFormOrFail($rules);

        $log = CashRequestLog::findOrFail($params['id']);
        if ($log->status == 0) {
            return $this->form->error('待处理提现申请不能删除！');
        }

        return $this->form->deleteMessage($log->delete());
    }

    /**
     * 审核
     */
    public function cashAudit()
    {
        $rules = [
            ['id', 'required|integer'],
            ['reason', 'string'],
            ['status', 'required|min:1|max:2'],
        ];

        $params = $this->form->camelFormOrFail($rules);

        $log = CashRequestLog::findOrFail($params['id']);
        if ($log->status != 0) {
            return $this->form->error('已处理，请勿重复操作');
        }
        if ($params['status'] == 2 && empty($params['reason'])) {
            return $this->form->error('请填写拒绝理由');
        }

        DB::transaction(function () use ($params, $log) {

            $log->fill(['status' => $params['status']])->save();

            // 拒绝
            if ($params['status'] == 2) {

                // 退还积分
                $user = User::findOrFail($log->uid);
                $user->increment('integral', $log->changed_integral);

                // 新增积分变动记录
                $create = [
                    'uid' => $log->uid,
                    'cash_log_id' => $params['id'],
                    'changed_integral' => $log->changed_integral,
                    'current_integral' => $user->integral,
                    'flag' => 7,
                    'type' => 1,
                    'is_finished' => 1,
                    'reason' => $params['reason'],
                    'desc' => '提现申请退还'
                ];
                UserIntegralLog::create($create);

            } elseif ($params['status'] == 1) {

                // 通过
                UserIntegralLog::where('uid', $log->uid)->where('cash_log_id', $log->id)->update(['is_finished' => 1]);
            }
        });

        return $this->form->success('审核成功');
    }
}